<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
        offset="50"
      >
        <ArticleItem
          v-for="obj in list"
          :key="obj.art_id"
          :artObj="obj"
          @dislike="onDislike"
          @reportFn="onReport"
          @click.native="toDetail(obj.art_id)"
        ></ArticleItem
      ></van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import ArticleItem from '../../../components/ArticleItem.vue'
import { getAllArticleListAPI, dislikeArticleAPI, reportArticleApi } from '@/api'
import Notify from '@/ui/Notify.js'
export default {
  props: {
    // list: Array
    channelId: Number
  },
  components: { ArticleItem },
  data () {
    return {
      list: [],
      loading: false, // 底部加载的状态
      finished: false, // 底部完成的状态
      theTime: new Date().getTime(),
      isLoading: true // 底部的加载状态
    }
  },
  async created () {
    const res = await getAllArticleListAPI({
      channel_id: this.channelId,
      timestamp: this.theTime
    })
    this.list = res.data.data.results
    this.theTime = res.data.data.pre_timestamp
  },
  methods: {
    async getArticleList () {
      const res = await getAllArticleListAPI({
        channel_id: this.channelId,
        timestamp: this.theTime
      })
      console.log(res)
      this.list = [...this.list, ...res.data.data.results]
      this.theTime = res.data.data.pre_timestamp
      this.loading = false
      if (res.data.data.pre_timestamp === null) {
        this.finished = true
      }
      this.isLoading = false
    },
    //   底部加载的事件方法
    onLoad () {
      if (this.list.length === 0) {
        this.loading = false
        return
      }
      this.getArticleList()
    },
    // 顶部刷新事件
    async onRefresh () {
      this.list = []
      this.timestamp = new Date().getTime()
      this.getArticleList()
    },
    // 反馈-不感兴趣
    async onDislike (id) {
      try {
        await dislikeArticleAPI({ artId: id })
        Notify({ type: 'success', message: '反馈成功' })
      } catch (error) {
        Notify({ type: 'warning', message: '反馈失败' })
      }
    },
    // 反馈-垃圾内容
    async onReport (id, value) {
      await reportArticleApi({
        target: id,
        type: value
      })
      Notify({ type: 'success', message: '举报成功' })
    },
    // 跳转到详情页
    toDetail (id) {
      this.$router.push(`/detail?art_id=${id}`)
    }

  }
}
</script>

<style>
</style>
